package cn.maihe.elg.operation.supports.electronicseal.kt.cloudsign.service;

import cn.maihe.elg.operation.supports.electronicseal.kt.KtConfig;
import cn.maihe.elg.operation.supports.electronicseal.kt.cloudsign.BaseKtCloudSignService;
import cn.maihe.elg.operation.supports.electronicseal.kt.cloudsign.dto.ListByMobileReqDTO;
import cn.maihe.elg.operation.supports.electronicseal.kt.cloudsign.dto.ListByMobileRespDTO;
import cn.maihe.elg.operation.supports.electronicseal.kt.cloudsign.enums.CloudSignInterfaceEnum;
import cn.maihe.elg.operation.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Description 根据手机号获取证章列表
 * @Author WJH
 * @Date 2022/09/19
 */
@Service
@Slf4j
public class ListByMobileService extends BaseKtCloudSignService<ListByMobileReqDTO, ListByMobileRespDTO> {
    public ListByMobileService(KtConfig ktConfig) {
        super(ktConfig);
    }

    @Override
    protected CloudSignInterfaceEnum serviceName() {
        return CloudSignInterfaceEnum.listByMobile;
    }

    public ListByMobileRespDTO list(String authMobile) {
        ListByMobileReqDTO reqDTO = new ListByMobileReqDTO();
        reqDTO.setMobile(authMobile);

        ListByMobileRespDTO respDTO = this.doService(authMobile, reqDTO);
        return respDTO;
    }

    public boolean existsUserId(String authMobile, List<String> dbUserIds) {
        ListByMobileRespDTO respDTO = this.list(authMobile);
        if (!CollectionUtils.isEmpty(respDTO.getList())) {
            AtomicBoolean tf = new AtomicBoolean(false);
            dbUserIds.forEach(dbUserId -> {
                boolean exists = respDTO.getList().stream().filter(o -> StringUtil.equals(dbUserId, o.getUserId())).count() > 0;
                if (!exists) {
                    throw new RuntimeException("该授权手机号码关联的证书Id不存在当前证书userId");
                }
                tf.set(true);
            });
            return tf.get();
        } else {
            throw new RuntimeException("查无此授权手机号码关联的证书Id");
        }
    }


}
